#include "calibrationobjectiveform.h"
#include "ui_calibrationobjectiveform.h"
#include <QPainter>
#include <QStyleOption>

CalibrationObjectiveForm::CalibrationObjectiveForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CalibrationObjectiveForm)
{
    ui->setupUi(this);
    this->setFixedHeight(292);
    this->setProperty("form","backgroundform");
    ui->scrollAreaWidgetContents->setProperty("form","backgroundform");
    ui->leftBorderForm->SetLabelIcon("objectiveform");
    ui->leftBorderForm->UpdateShowType(3);
    ui->stackedWidget->setCurrentIndex(0);
    ui->scrollArea->hide();


    ui->toolButton_contractExpand_csi->setProperty("button","expand");
    ui->toolButton_contractExpand_psi->setProperty("button","expand");
    ui->toolButton_contractExpand_flat->setProperty("button","expand");
    ui->toolButton_contractExpand_fusion->setProperty("button","expand");
    ui->toolButton_contractExpand_skewing->setProperty("button","expand");
    ui->toolButton_contractExpand_zFactor->setProperty("button","expand");
    ui->toolButton_contractExpand_zFactorLs->setProperty("button","expand");
    ui->toolButton_contractExpand_chromatism->setProperty("button","expand");
    ui->toolButton_contractExpand_confocalFlattening->setProperty("button","expand");
    ui->toolButton_contractExpand_amplificationFactor->setProperty("button","expand");

    ui->pushButton_tool->setIcon(QIcon(":/res/CalibrationObjectiveForm/pushButton_tool_checked.png"));
    ui->pushButton_restore->setIcon(QIcon(":/res/CalibrationObjectiveForm/pushButton_restore.png"));
    ui->pushButton_delete->setIcon(QIcon(":/res/CalibrationObjectiveForm/pushButton_delete.png"));

    ui->label_status_csi->setProperty("model","green");
    ui->label_status_psi->setProperty("model","green");
    ui->label_status_flat->setProperty("model","green");
    ui->label_status_fusion->setProperty("model","green");
    ui->label_status_skewing->setProperty("model","green");
    ui->label_status_zFactor->setProperty("model","green");
    ui->label_status_zFactorLs->setProperty("model","green");
    ui->label_status_chromatism->setProperty("model","green");
    ui->label_status_confocalFlattening->setProperty("model","green");
    ui->label_status_amplificationFactor->setProperty("model","green");



}

CalibrationObjectiveForm::~CalibrationObjectiveForm()
{
    delete ui;
}

void CalibrationObjectiveForm::setCurrentHeight(bool checked, QWidget *_inWidget,bool isChangeStackedWidget)
{
    if(checked)
    {
        // 显示 _inWidget 时，设置 scrollArea 高度为原始高度加上 _inWidget 的高度
        _inWidget->show();
        _inWidget->adjustSize();
        _inWidget->updateGeometry();

        int originalScrollAreaHeight = ui->scrollArea->height() + _inWidget->height();

        if(isChangeStackedWidget)
        {
            int originalStackedHeight = ui->stackedWidget->height() + _inWidget->height();
            ui->stackedWidget->setFixedHeight(originalStackedHeight);
        }

        ui->scrollArea->setFixedHeight(originalScrollAreaHeight);
        this->setFixedHeight(ui->widget_default->height() + ui->scrollArea->height() + 30);

    }
    else {
        // 隐藏 widget_skewing 时，设置 scrollArea 高度为原始高度
        int originalScrollAreaHeight = ui->scrollArea->height() - _inWidget->height();
        if(isChangeStackedWidget)
        {
            int originalStackedHeight = ui->stackedWidget->height() - _inWidget->height();
            ui->stackedWidget->setFixedHeight(originalStackedHeight);
        }
        ui->scrollArea->setFixedHeight(originalScrollAreaHeight);
        this->setFixedHeight(ui->widget_default->height() + ui->scrollArea->height() + 30);
        _inWidget->hide();
    }
}

void CalibrationObjectiveForm::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter painter(this);
    style()->drawPrimitive(QStyle::PE_Widget,&opt,&painter,this);
//    ui->scrollAreaWidgetContents->style()->drawPrimitive(QStyle::PE_Widget,&opt,&painter,ui->scrollAreaWidgetContents);
    QWidget::paintEvent(event);
}

void CalibrationObjectiveForm::on_pushButton_tool_clicked(bool checked)
{
    if(!checked)
    {
        this->setFixedHeight(292);
        ui->scrollArea->hide();
        ui->pushButton_tool->setIcon(QIcon(":/res/CalibrationObjectiveForm/pushButton_tool.png"));
    }
    else
    {
        initExpendWidget(ui->comboBox->currentIndex());
        ui->scrollArea->show();
        ui->pushButton_tool->setIcon(QIcon(":/res/CalibrationObjectiveForm/pushButton_tool_checked.png"));
    }
}

void CalibrationObjectiveForm::initExpendWidget(int _index)
{
    ui->toolButton_contractExpand_skewing->setChecked(false);
    ui->widget_skewing->hide();
    ui->toolButton_contractExpand_flat->setChecked(false);
    ui->widget_flat->hide();
    ui->toolButton_contractExpand_amplificationFactor->setChecked(false);
    ui->widget_amplificationFactor->hide();
    if(_index<3)
    {
        ui->toolButton_contractExpand_confocalFlattening->setChecked(false);
        ui->widget_confocalFlattening->hide();
        ui->toolButton_contractExpand_chromatism->setChecked(false);
        ui->widget_chromatism->hide();
        ui->toolButton_contractExpand_fusion->setChecked(false);
        ui->widget_fusion->hide();
        ui->toolButton_contractExpand_zFactorLs->setChecked(false);
        ui->widget_zFactorLs->hide();
        ui->stackedWidget->setCurrentIndex(0);
//        this->setFixedHeight(300+30*7);
        ui->scrollArea->setFixedHeight(30*9);
        ui->stackedWidget->setFixedHeight(30*4);
        this->setFixedHeight(ui->widget_default->height()+ui->scrollArea->height()+30);
    }
    else
    {
        ui->toolButton_contractExpand_psi->setChecked(false);
        ui->widget_psi->hide();
        ui->toolButton_contractExpand_csi->setChecked(false);
        ui->widget_csi->hide();
        ui->toolButton_contractExpand_zFactor->setChecked(false);
        ui->widget_zFactor->hide();
        ui->stackedWidget->setCurrentIndex(1);
//        this->setFixedHeight(300+30*6);
        ui->scrollArea->setFixedHeight(30*8);
        ui->stackedWidget->setFixedHeight(30*3);
        this->setFixedHeight(ui->widget_default->height()+ui->scrollArea->height()+30);
    }
}

void CalibrationObjectiveForm::on_comboBox_currentIndexChanged(int index)
{
    if(!ui->scrollArea->isHidden())
    {
        initExpendWidget(index);
    }
}

void CalibrationObjectiveForm::on_toolButton_contractExpand_skewing_clicked(bool checked)
{
    setCurrentHeight(checked,ui->widget_skewing);
}

void CalibrationObjectiveForm::on_stackedWidget_currentChanged(int arg1)
{
    if(arg1 == 0)
        ui->stackedWidget->setFixedHeight(590);
    else {
        ui->stackedWidget->setFixedHeight(460);
    }
}

void CalibrationObjectiveForm::on_toolButton_contractExpand_flat_clicked(bool checked)
{
    setCurrentHeight(checked,ui->widget_flat);
}

void CalibrationObjectiveForm::on_toolButton_contractExpand_confocalFlattening_clicked(bool checked)
{
    setCurrentHeight(checked,ui->widget_confocalFlattening,true);
}

void CalibrationObjectiveForm::on_toolButton_contractExpand_chromatism_clicked(bool checked)
{
    setCurrentHeight(checked,ui->widget_chromatism,true);
}

void CalibrationObjectiveForm::on_toolButton_contractExpand_fusion_clicked(bool checked)
{
    setCurrentHeight(checked,ui->widget_fusion,true);
}

void CalibrationObjectiveForm::on_toolButton_contractExpand_zFactorLs_clicked(bool checked)
{
    setCurrentHeight(checked,ui->widget_zFactorLs,true);
}

void CalibrationObjectiveForm::on_toolButton_contractExpand_amplificationFactor_clicked(bool checked)
{
    setCurrentHeight(checked,ui->widget_amplificationFactor);
}

void CalibrationObjectiveForm::on_toolButton_contractExpand_psi_clicked(bool checked)
{
    setCurrentHeight(checked,ui->widget_psi,true);
}

void CalibrationObjectiveForm::on_toolButton_contractExpand_csi_clicked(bool checked)
{
    setCurrentHeight(checked,ui->widget_csi,true);
}

void CalibrationObjectiveForm::on_toolButton_contractExpand_zFactor_clicked(bool checked)
{
    setCurrentHeight(checked,ui->widget_zFactor,true);
}

void CalibrationObjectiveForm::on_pushButton_restore_clicked(bool checked)
{
    if(!checked)
    {
        ui->pushButton_restore->setIcon(QIcon(":/res/CalibrationObjectiveForm/pushButton_restore.png"));
    }
    else
    {
        ui->pushButton_restore->setIcon(QIcon(":/res/CalibrationObjectiveForm/pushButton_restore_checked.png"));
    }
}

void CalibrationObjectiveForm::on_pushButton_delete_clicked(bool checked)
{
    if(!checked)
    {
        ui->pushButton_delete->setIcon(QIcon(":/res/CalibrationObjectiveForm/pushButton_delete.png"));
    }
    else
    {
        ui->pushButton_delete->setIcon(QIcon(":/res/CalibrationObjectiveForm/pushButton_delete_checked.png"));
    }
}
